package hotLeetcode;

import javax.imageio.metadata.IIOMetadataFormatImpl;
import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.List;
import java.util.TooManyListenersException;

public class H48Leetcode {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        int m = matrix.length, n = matrix[0].length;
        int left = 0, right = n - 1, top = 0, bot = m - 1;

        while (left <= right && top <= bot) {
            for (int i = left; i <= right; i++) {
                list.add(matrix[top][i]);
            }
            top++;

            for (int i = top; i <= bot; i++) {
                list.add(matrix[i][right]);
            }
            right--;

            if (top <= bot) {
                for (int i = right; i >= left; i--) {
                    list.add(matrix[bot][i]);
                }
            }
            bot--;
            if (left <= right) {
                for (int i = bot; i >= top; i--) {
                    list.add(matrix[i][left]);
                }
            }

            left++;
        }
//        if (m == n) {
//            list.add(matrix[m / 2][n / 2]);
//        }




        return list;


    }

    public static void main(String[] args) {
        H48Leetcode h48Leetcode = new H48Leetcode();
        int[][] matrix = {{1},{2},{3},{4}};
        System.out.println(h48Leetcode.spiralOrder(matrix));

    }
}
